<template>
  <div>
    <el-card>

      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-form label-width="80px">

        <el-form-item label="状态：">
          <el-radio-group v-model="reqParams.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="频道：">
          <my-channels :reqParams="reqParams"></my-channels>
        </el-form-item>

        <el-form-item label="日期：">
          <el-date-picker @change="changeDate" v-model="dateArr" type="daterange" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="">
          <el-button type="primary" @click="filterArticles">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 搜索结果 -->
    <el-card style="margin-top: 20px;">
      <div slot="header" class="clearfix">
        <span>根据筛选条件共查询到 {{ total }} 条结果：</span>
      </div>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image style="width: 200px; height: 150px" :src="scope.row.cover.images[0]">
              <div slot="error" class="name-wrapper">
                <img src="@/assets/error.png" style="width: 200px; height: 150px">
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="400px"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status === 1">待审核</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status === 3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status === 4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="pubdate"></el-table-column>
        <el-table-column label="阅读数" width=120px prop="read_count"></el-table-column>
        <el-table-column label="评论数" width=120px prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" width=120px prop="like_count"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle plain @click="$router.push(`/publish?id=${scope.row.id}`)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle plain
              @click="deleteArticleBtn(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination style="margin-top:20px" background layout="prev, pager, next" :total="total"
      :current-page="reqParams.page" :page-size="reqParams.per_page" @current-change="togglePage">
    </el-pagination>
  </div>
</template>

<script>
import MyChannels from '../../components/my-channels.vue'
import { getArticleListInfoAPI, deleteArticleAPI } from '@/api/index.js'
export default {
  name: 'articleIndex',
  components: {
    MyChannels
  },
  data () {
    return {
      reqParams: {
        // 单选框的状态
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1,
        per_page: 10
      },
      channels: null,
      dateArr: null,
      articles: [],
      total: null
    }
  },
  created () {
    // 调用获取文章列表接口
    this.loadArticle()
  },
  methods: {
    // 获取文章列表接口
    async loadArticle () {
      try {
        const { data: res } = await getArticleListInfoAPI(this.reqParams)
        console.log(res)
        this.articles = res.data.results
        this.total = res.data.total_count
      } catch (error) {
        console.log(error)
      }
    },
    // 分页功能
    togglePage (changedPage) {
      // 将page改为需要展示的页面然后调接口
      this.reqParams.page = changedPage
      this.loadArticle()
    },
    // 筛选功能
    filterArticles () {
      // 每次筛选从第一页开始展示
      this.reqParams.page = 1
      this.loadArticle()
    },
    // 选择时间触发的事件
    changeDate (dateArr) {
      if (dateArr) {
        this.reqParams.begin_pubdate = dateArr[0]
        this.reqParams.end_pubdate = dateArr[1]
      } else {
        this.reqParams.begin_pubdate = null
        this.reqParams.end_pubdate = null
      }
    },
    // 删除功能
    deleteArticleBtn (currentId) {
      this.$confirm('此操作将永久删除该文章，是否继续？', '温馨提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async () => {
        await deleteArticleAPI(currentId)
        this.$message.success('删除成功')
        this.loadArticle()
      }).catch(() => {
      })
    }
  }
}
</script>

<style>

</style>
